<?php
	/**
	 * Extension configuration that describes how the extension engine handles this extension.
	 * @author IMP
	 */
	
	class ExtensionConfiguration {
		private $name = null,
				$version,
				$description = '',
				$autoload = false,
				$include = array(),
				$extensions = array(),
				$classes = array(),
				$functions = array();
		
		public function __construct($name, $version, $description = '', $autoload = false, array $include = array(), array $extension = array(), array $classes = array(), array $functions = array()){
			$this->setName($name);
			$this->setVersion($version);
			$this->setDescription($description);
			$this->setAutoload($autoload);
			$this->include = self::formatIncludes($this, $include);
			$this->extensions = array_values($extension);
			$this->classes = array_values($classes);
			$this->functions = array_values($functions);
		}
		
		public function setName($name){
			if(!is_string($name)) throw new ExtensionConfigurationException('Name has to be a string.');
			if(count(explode("\n", $name)) > 1 || count(explode("\r", $name)) > 1) throw new ExtensionConfigurationException('The name may not contain line breaks.');
			$this->name = $name;
		}
		
		public function setVersion($version){
			if(count(explode("\n", $version)) > 1 || count(explode("\r", $version)) > 1) throw new ExtensionConfigurationException('The version may not contain line breaks.');
			$this->version = $version;
		}
		
		public function setDescription($description){
			if(!is_string($description)) throw new ExtensionConfigurationException('Description has to be a string.');
			if(count(explode("\n", $description)) > 1 || count(explode("\r", $description)) > 1) throw new ExtensionConfigurationException('The description may not contain line breaks.');
			$this->description = $description;
		}
		
		public function setAutoload($autoload){
			if(!is_bool($autoload)) throw new ExtensionConfigurationException('Autoload option has to be a boolean.');
			$this->autoload = $autoload;
		}
		
		public function getName(){
			return $this->name;
		}
		
		public function getVersion(){
			return $this->version;
		}
		
		public function isAutoloaded(){
			return $this->autoload;
		}
		
		public function getIncludes(){
			return array_values($this->include);
		}
		
		public function getExtensions(){
			return $this->extensions;
		}
		
		public function generateEntry(){
			$section = new IniSection($this->name);
			
			$section['version'] = $this->version;
			$section['description'] = $this->description;
			$section['autoload'] = $this->autoload;
			$section['include'] = array_values($this->include);
			$section['extension'] = $this->extensions;
			$section['classes'] = $this->classes;
			$section['functions'] = $this->functions;
			
			return $section;
		}
		
		private static function formatIncludes(self $b, array $includes){
			foreach($includes as $k => $include){
				if(!File::exists(PATH_EXTENSIONS . DS . $b->getName() . DS . $include)) throw new ExtensionConfigurationException('Include file "' . $include . '" does not exist.');
			}
			
			return count($includes) > 0 ? array_combine(array_values($includes), array_values($includes)) : array();
		}
	}
?>